package com.yun.zf.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.common.app.domain.SysUserAuth;
import com.yun.zf.system.mapper.SysUserAuthMapper;
import com.yun.zf.system.service.SysUserAuthService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 第三方登录表 服务实现类
 * </p>
 *
 * @author cj
 * @since 2020-09-23
 */
@Service
public class SysUserAuthServiceImpl extends ServiceImpl<SysUserAuthMapper, SysUserAuth> implements SysUserAuthService {

    @Override
    public SysUserAuth getUserByIphone(Long iphone) {
        QueryWrapper<SysUserAuth> authQueryWrapper = new QueryWrapper<>();
        authQueryWrapper.lambda().eq(SysUserAuth::getIphone,iphone);
        List<SysUserAuth> auths = baseMapper.selectList(authQueryWrapper);
        return  auths.size()>0?auths.get(0):null;
    }

    @Override
    public SysUserAuth getUserByIphone(Long iphone, String loginType) {
        QueryWrapper<SysUserAuth> authQueryWrapper = new QueryWrapper<>();
        authQueryWrapper.lambda().eq(SysUserAuth::getLoginType,loginType).eq(SysUserAuth::getIphone,iphone);
        List<SysUserAuth> auths = baseMapper.selectList(authQueryWrapper);
        return  auths.size()>0?auths.get(0):null;
    }

    @Override
    public SysUserAuth getUserByOpenId(String openId,String loginType) {
        QueryWrapper<SysUserAuth> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SysUserAuth::getLoginType,loginType).eq(SysUserAuth::getOpenId,openId);
        List<SysUserAuth> auths = baseMapper.selectList(wrapper);
        return  auths.size()>0?auths.get(0):null;
    }
    @Override
    @Transactional
    public int updateAuthByOpenId(SysUserAuth auth) {
        QueryWrapper<SysUserAuth> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SysUserAuth::getOpenId,auth.getOpenId());
        return baseMapper.update(auth,wrapper);
    }
}
